
package org.zfes.snowier.cloud.zbss.pmp.router.service.ma;

import java.util.List;
import java.util.Map;

import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.zfes.snowier.core.data.ComboboVo;
import org.zfes.snowier.core.data.DataSet;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.web.controller.BaseController;
import org.zfes.snowier.auth.biz.model.AuthUser;
import org.zfes.snowier.auth.biz.service.IAuthUserService;
import org.zfes.snowier.auth.shiro.util.ShiroMD5Util;
import org.zfes.snowier.cloud.zbss.pmp.dto.AccountDto;
import org.zfes.snowier.cloud.zbss.pmp.model.PmpCloudMember;
import org.zfes.snowier.cloud.zbss.pmp.service.IPmpCloudMemberService;

@Controller ("org.zfes.snowier.cloud.zbss.pmp.router.service.ma.pmpCloudMemberController")
@RequestMapping("/pmp/service/ma/pmpCloudMember")
public class PmpCloudMemberController extends BaseController {

	
	@Autowired
	private IPmpCloudMemberService pmpCloudMemberService;
	@Autowired 
	private IAuthUserService authUserService;
	
	
	 @RequestMapping(value="/loadPmpCloudMemberDataSet", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody  Object loadPmpCloudMemberDataSet(@RequestParam Map<String,Object> params){
	    DataSet dataSet=  pmpCloudMemberService.loadPmpCloudMemberDataSet(params);
	    return ajaxQuery("dataSet", dataSet);
    }
	 
	 @RequestMapping(value="/loadCloudMemberComboboVo", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	    public @ResponseBody  Object loadDeduceLineComboboVo(Boolean firstEmpty){
			 List<ComboboVo>  vo=pmpCloudMemberService.loadComboboVo(firstEmpty);
		    return ajaxQuery("comboboVo", vo);
	    }
	
	
    @RequestMapping(value="/createPmpCloudMember", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public  @ResponseBody Object createPmpCloudMember(AccountDto accountDto){
		
    	ZBeanUtil.validateBean(accountDto);
    	String password=ShiroMD5Util.genSetp1Password(accountDto.getPassword());
		String salt=ShiroMD5Util.genRandomSalt();
		 password=ShiroMD5Util.genSetp2Password(password, salt);
		 accountDto.setSalt(salt);
		 accountDto.setPassword(password);
		 
		pmpCloudMemberService.createPmpCloudMember(accountDto);
    	return ajaxDoneSuccess("数据操作成功 ");
    }
	
	
    @RequestMapping(value="/loadPmpCloudMember", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody  Object loadPmpCloudMember(Long id){
		Optional<PmpCloudMember> ops = pmpCloudMemberService.loadPmpCloudMemberById(id);
		return ajaxQuery("pmpCloudMember",ops);
    }

	
    @RequestMapping(value="/deletePmpCloudMember", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody  Object deletePmpCloudMember(Long id){
		pmpCloudMemberService.deletePmpCloudMember(id);
		return ajaxDoneSuccess("数据操作成功 ");
    }
    @RequestMapping(value="/setPositionLevel", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody  Object  setPositionLevel(Long id,Byte PositionLevel) {
    	pmpCloudMemberService.setPositionLevel(id, PositionLevel);
    	return ajaxDoneSuccess("数据操作成功 ");
    }
    @RequestMapping(value="/setUserType", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody  Object  setUserType(Long id,Byte userType) {
    	pmpCloudMemberService.setUserType(id, userType);
    	return ajaxDoneSuccess("数据操作成功 ");
    }
	
	
	@RequestMapping(value="/setpwd", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody  Object   setpwd(Long userId, String newPassword, String repeatNewPassword) {
    	ZAssert.notNull(userId, "未获取到用户信息");
		ZAssert.hasText(newPassword, "请输入新密码");
		ZAssert.equals(newPassword, repeatNewPassword, "两次密码不一致");
		Optional<AuthUser> userOp=authUserService.loadAuthUser(userId);
		ZAssert.notEmpty(userOp, "未获取到用户信息");
    	pmpCloudMemberService.setpwd(userOp.get().getAccount(), newPassword);
    	return ajaxDoneSuccess("数据操作成功 ");
    }
    
}